package com.idc.quartz.controller;

import com.idc.quartz.job.SampleJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

@RestController
public class JobController {

    @Autowired
    private Scheduler scheduler;

    @GetMapping("/schedule")
    public void schedule() throws SchedulerException {
        // 任务得开始时间   结束时间  发布得内容 设备
        publishJob();

    }


    private void  publishJob() throws SchedulerException {
        String deviceId="12rqwvv";
        JobDetail job = newJob(SampleJob.class)
                .withIdentity("job1", "group1")
                .usingJobData("deviceId",deviceId)
                .usingJobData("cont","{id:njjj,colr}")
                .build();


        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(1);


        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault())
                .toInstant());



        SimpleTrigger trigger = newTrigger().forJob(job)
                .withIdentity("Qrtz_Trigger")
                .withDescription("Sample trigger")
                .startAt(date)
                .withSchedule(simpleSchedule().withRepeatCount(0).withIntervalInSeconds(1))
                .build();

        scheduler.scheduleJob(job, trigger);
    }
}
